package com.jch.dm8_thread_specific_storage.example.tomcat.demo.memoryleaking;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

// ThreadLocal内存泄漏示例代码
public class MemoryLeakingServlet extends HttpServlet {

    private final static ThreadLocal<Counter> TL_COUNTER = new ThreadLocal<Counter>(){
        @Override
        protected Counter initialValue() {
            return new Counter();
        }
    };

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter pwr = resp.getWriter();
        pwr.write(String.valueOf(TL_COUNTER.get().getAndIncrement()));
        pwr.close();
    }
}

class Counter {

    private int i = 0;

    public int getAndIncrement() {
        return (i++);
    }

}
